package luanluan.utils.load 
{
	import flash.display.DisplayObject;
	import flash.system.LoaderContext;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	
	/**
	 * 封装loader
	 * @author luanluan
	 */
	public class LLoader extends Loader implements ILoader
	{
		//*********************************************************//
		//	*各种事件处理函数
		private var _progressFunc:Function;
		
		private var _completeFunc:Function;
		
		private var _openFunc:Function;
		
		private var _ioErrorFunc:Function;
		
		//*********************************************************//
		//	*加载对象的地址
		private var _url:String;
		
		/**
		 * 封装的Loader类
		 */
		public function LLoader(url:String = "") 
		{
			if (url != "")
				_url = url;
		}
		
		/**
		 * 开始加载
		 * @param	url
		 */
		public function start(url:String = ""):void 
		{
			if (url != "")
				_url = url;
			super.load(new URLRequest(_url));
		}
		
		/**
		 * 设置加载时处理进度的函数
		 * @param	func
		 */
		public function setProgress(func:Function):void
		{
			_progressFunc = func;
			this.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS , progressHandle);
		}
		
		/**
		 * 设置加载完成时处理函数
		 * @param	func
		 */
		public function setComplete(func:Function):void
		{
			_completeFunc = func;
			this.contentLoaderInfo.addEventListener(Event.COMPLETE , completeHandle);
		}
		
		/**
		 * 设置加载开始时处理函数
		 * @param	func
		 */
		public function setOpen(func:Function):void
		{
			_openFunc = func;
			this.contentLoaderInfo.addEventListener(Event.OPEN , openHandle);
		}
		
		/**
		 * 设置加载发生溜错误的处理函数
		 * @param	func
		 * @param	...params
		 */
		public function setIOError(func:Function):void
		{
			_ioErrorFunc = func;
			this.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR , ioErrorHandle);
		}
		
		private function ioErrorHandle(e:IOErrorEvent):void 		{			_ioErrorFunc();		}
		
		private function openHandle(e:Event):void 					{			_openFunc();		}
		
		private function completeHandle(e:Event):void 				{			_completeFunc();	}
		
		private function progressHandle(e:ProgressEvent):void 		{			_progressFunc();	}
		
		/**
		 * 获取所加载的url
		 */
		public function get url():String 							
		{			
			return _url;		
		}
		
		/**
		 * 获取已加载的字节数
		 */
		public function get bytesLoaded():uint
		{
			return this.contentLoaderInfo.bytesLoaded;
		}
		
		/**
		 * 获取所加载对象总字节数
		 */
		public function get bytesTotal():uint
		{
			return this.contentLoaderInfo.bytesTotal;
		}
		
	}

}